package com.pro.interceptor;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class MyInterceptor extends HandlerInterceptorAdapter {
	private String mappingURL;// 利用正则映射到需要拦截的路径
	private final static String[] ignoreUrls = { "login.do", "userinfo.do", "left.do", "top.do","enterModifyPwd.do" ,"addUser.do"};

//	@Resource
//	public LogDao logDao;

	public void setMappingURL(String mappingURL) {
		this.mappingURL = mappingURL;
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception arg3) throws Exception {
//		System.out.println(handler);
//		if (HandlerMethod.class.equals(handler.getClass())) {
//			HandlerMethod handlerMethod = (HandlerMethod) handler;
//			LogMethod logMethod = handlerMethod.getMethodAnnotation(LogMethod.class);
////			handlerMethod.get
//			if (logMethod != null) {
//				String value = logMethod.value();
//				User user = (User) request.getSession().getAttribute("user");
//				Integer accountId = user == null ? 0 : user.getId();
//				String account = "EAccount_" + accountId;
//				logDao.addObject(new Log(accountId, account, value, new Date()));
//			}
//		}
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
//		System.out.println(arg2);
//		String url = request.getRequestURL().toString();
//		User user = (User) request.getSession().getAttribute("user");
//		for (String ignoreUrl : ignoreUrls) {
//			if (url.contains(ignoreUrl)) {
//				return true;
//			}
//		}
//		if (user == null) {
//			request.getRequestDispatcher("/login.do").forward(request, response);
//			return false;
//		}
		return true;
	}

}
